<template>
  <el-dialog
    class="base-dialog user-group-dialog"
    :custom-class="'panna-dialog_small'"
    :title="title"
    :visible.sync="dialogVisible"
    :append-to-body="true"
    :before-close="closeDialog"
  >
    <p slot="title">设置价格</p>
    <div></div>

    <el-form :rules="rules">
      <el-form-item
        v-for="(item, index) in formArray"
        :key="index"
        :label="item.title"
        :label-width="formLabelWidth"
        :prop="'price' + index"
      >
        <el-input-number
          v-if="item.tag != 4"
          v-model="item.price"
          style="float: left; width: 71%"
          :controls="false"
          :precision="2"
          :min="0"
          :max="100000000"
        ></el-input-number>

        <el-input
          v-else
          v-model="item.price"
          style="float: left; width: 71%"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="base-dialog-footer">
      <el-button type="primary" @click="setPrice(item)">确 定</el-button>
      <el-button @click="closeDialog">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { BeatchUpdatePrice } from "../../../api/SysBusiness/SysBusinessDetectionItem.js";

export default {
  name: "DetectionItemBeatchUpdateDialog",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    detectionItemNameArray: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    const validatorExpireNum = (rule, value, callback) => {
      this.checkExpireNum(value, callback);
    };

    return {
      agreementPrice: "",
      firstAgreementPrice: "",
      secondAgreementPrice: "",
      thirdAgreementPrice: "",
      explain: "",
      formDialogVisible: false,
      title: "设置价格",
      formLabelWidth: "100px",
      formArray: [],
      requestArry: [],
      rules: {
        price0: [
          { required: true, validator: validatorExpireNum, trigger: "blur" },
        ],
      },
    };
  },
  watch: {},
  created() {
    this.init();
  },
  methods: {
    checkExpireNum(value, callback) {
      var price = this.formArray[0].price;
      if (price === "" || price === undefined) {
        callback("单价必须填写!");
        return;
      } else {
        callback();
      }
    },
    init() {
      this.formDialogVisible = this.dialogVisible;
      for (var i = 0; i <= 4; i++) {
        var obj = {
          price: "",
          title: "",
          tag: "",
        };
        if (i === 0) {
          obj.title = "单价(元)";
          obj.tag = 0;
          obj.price = "";
        }
        if (i === 1) {
          obj.title = "协议价1(元)";
          obj.tag = 1;
          obj.price = "";
        }
        if (i === 2) {
          obj.title = "协议价2(元)";
          obj.tag = 2;
          obj.price = "";
        }
        if (i === 3) {
          obj.title = "协议价3(元)";
          obj.tag = 3;
          obj.price = "";
        }
        if (i === 4) {
          obj.title = "备注/说明";
          obj.tag = 4;
          obj.price = "";
        }
        this.formArray.push(obj);
      }
    },
    setPrice() {
      if (
        this.formArray[0].price === "" ||
        this.formArray[0].price === undefined
      ) {
        return;
      }
      var obj = {};
      obj.detectionItemNameArray = this.detectionItemNameArray;
      for (var i = 0; i < this.formArray.length; i++) {
        if (this.formArray[i].tag === 0) {
          obj.agreementPrice = this.formArray[i].price;
        }
        if (this.formArray[i].tag === 1) {
          obj.firstAgreementPrice = this.formArray[i].price;
        }
        if (this.formArray[i].tag === 2) {
          obj.secondAgreementPrice = this.formArray[i].price;
        }
        if (this.formArray[i].tag === 3) {
          obj.thirdAgreementPrice = this.formArray[i].price;
        }
        if (this.formArray[i].tag === 4) {
          obj.explain = this.formArray[i].price;
        }
      }

      this.$confirm(
        "批量设置价格将清空原有价格设置，请确认是否继续本操作?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        }
      )
        .then(() => {
          BeatchUpdatePrice(obj)
            .then((res) => {
              this.handleClose();
            })
            .catch((res) => {});
        })
        .catch(() => {});
    },
    handleClose() {
      this.$emit("input", false);
    },
    closeDialog() {
      this.$emit("getList");
      this.$emit("update:dialogVisible", false);
    },
  },
};
</script>

